package com.anyonavinfo.cpaddemo.data.musicinfo.source;


import com.anyonavinfo.cpaddemo.data.musicinfo.MusicInfo;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.local.MusicInfoLocalDataSource;

<<<<<<< HEAD

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
=======
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232

/**
 * Created by ZuiC on 2017/1/23.
 */

public class MusicInfoRepository implements MusicInfoDataSource {

    private static MusicInfoRepository INSTANCE;
    private MusicInfoLocalDataSource mMusicInfoLocalDataSource;
<<<<<<< HEAD
    //private Map<Integer, MusicInfo> mCachedMusicInfo;
    private int curMusic = 0;
    private int curSize;
    private int curPlayMode = 0;
    private int curPlayStatus = 0;
    private List<MusicInfo> mCachedMusicInfo;

    public static MusicInfoRepository getInstance() {

        if (INSTANCE == null) {
            INSTANCE = new MusicInfoRepository();
=======
    private Map<Long, MusicInfo> mCachedMusicInfo;

    public static MusicInfoRepository getInstance(MusicInfoLocalDataSource musicInfoLocalDataSource) {

        if (INSTANCE == null) {
            INSTANCE = new MusicInfoRepository(musicInfoLocalDataSource);
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
        }
        return INSTANCE;
    }

<<<<<<< HEAD
    private MusicInfoRepository(){
        this.mMusicInfoLocalDataSource = MusicInfoLocalDataSource.getInstance();
=======
    private MusicInfoRepository(MusicInfoLocalDataSource musicInfoLocalDataSource){
        this.mMusicInfoLocalDataSource = musicInfoLocalDataSource;
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    }


    @Override
    public void loadMusicInfo(final LoadMusicInfoCallBack callBack) {
<<<<<<< HEAD
        if (mCachedMusicInfo != null) {
            callBack.onMusicInfoLoaded(mCachedMusicInfo);
=======
        if (mCachedMusicInfo != null){
            callBack.onMusicInfoLoaded(new ArrayList<>(mCachedMusicInfo.values()));
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
            return;
        }

        mMusicInfoLocalDataSource.loadMusicInfo(new LoadMusicInfoCallBack() {
            @Override
            public void onMusicInfoLoaded(List<MusicInfo> musicInfoList) {
                refreshCache(musicInfoList);
                callBack.onMusicInfoLoaded(musicInfoList);
            }
        });
    }

<<<<<<< HEAD
    @Override
    public void getNextMusicInfo(GetNextMusicInfoCallBack callBack) {
        curMusic = curMusic + 1 > curSize - 1 ? 0 : curMusic + 1;
        if (mCachedMusicInfo.get(curMusic) != null){
            callBack.onGetNextGetMusicInfo(mCachedMusicInfo.get(curMusic));
        }
    }

    @Override
    public void getPreviousMusicInfo(GetPreviousMusicInfoCallBack callBack) {
        curMusic = curMusic - 1 < 0 ? curSize - 1 : curMusic - 1;
        if (mCachedMusicInfo.get(curMusic) != null){
            callBack.onGetPreviousMusicInfo(mCachedMusicInfo.get(curMusic));
        }
    }

    @Override
    public void getCurMusicInfo(GetCurMusicInfoCallBack callBack) {
        if (mCachedMusicInfo.get(curMusic) != null){
            callBack.onGetCurMusicInfo(mCachedMusicInfo.get(curMusic));
        }
    }

    @Override
    public void changeCurPlayMode(ChangeCurPlayModeCallBack callBack) {
        switch (curPlayMode){
            case 0:
                curPlayMode = 1;
                break;
            case 1:
                curPlayMode = 2;
                break;
            case 2:
                curPlayMode = 0;
                break;
            default:
                break;
        }
        callBack.onChangeCurPlayMode(curPlayMode);
    }

    @Override
    public void getCurPlayMode(GetCurPlayModeCallBack callBack) {
        callBack.onGetCurPlayMode(curPlayMode);
    }

    @Override
    public void getCurPlayStatus(GetCurPlayStatusCallBack callBack) {
        callBack.onGetCurPlayStatus(curPlayStatus);
    }


    @Override
    public void changeCurPlayStatus(ChangeCurPlayStatusCallBack callBack) {
        curPlayStatus = curPlayStatus == 0 ? 1 : 0;
        callBack.onChangeCurPlayStatus(curPlayStatus);
    }

    @Override
    public void getMusicInfoWhenEnding(getMusicInfoWhenEndingCallBack callBack) {
        if (curPlayMode == 0){
            curMusic = curMusic + 1 > curSize - 1 ? 0 : curMusic + 1;
            if (mCachedMusicInfo.get(curMusic) != null){
                callBack.onGetMusicInfoWhenEnding(mCachedMusicInfo.get(curMusic));
            }
        }

        if (curPlayMode == 1){
            curMusic = new Random().nextInt(curSize);
            if (mCachedMusicInfo.get(curMusic) != null){
                callBack.onGetMusicInfoWhenEnding(mCachedMusicInfo.get(curMusic));
            }
        }

        if (curPlayMode == 2){
            if (mCachedMusicInfo.get(curMusic) != null){
                callBack.onGetMusicInfoWhenEnding(mCachedMusicInfo.get(curMusic));
            }
        }
    }

    @Override
    public void setCurMusic(SetCurMusicCallBack callBack, int selectedPosition) {
        curMusic = selectedPosition;
        callBack.onSetCurMusic(mCachedMusicInfo.get(curMusic));
    }

    @Override
    public void deleteMusic(DeleteMusicCallBack callBack, int selectedPosition) {
        boolean isSame = selectedPosition == curMusic;
        curMusic = curMusic > selectedPosition ? curMusic-- : curMusic;
        File file = new File(mCachedMusicInfo.get(selectedPosition).getPath());
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        mCachedMusicInfo.remove(selectedPosition);
        curSize = mCachedMusicInfo.size();
        boolean isClear = curMusic == 0;
        curMusic = curMusic > curSize - 1 ? 0 : curMusic;
        callBack.onDeleteMusic(mCachedMusicInfo, isSame, isClear);
    }

    @Override
    public void reloadMusicInfo(final ReloadMusicInfoCallBack callBack) {
        mMusicInfoLocalDataSource.loadMusicInfo(new LoadMusicInfoCallBack() {
            @Override
            public void onMusicInfoLoaded(List<MusicInfo> musicInfoList) {
                refreshCache(musicInfoList);
                callBack.onReloadMusicInfo(musicInfoList);
            }
        });
    }


    private void refreshCache(List<MusicInfo> musicInfoList){
        if (mCachedMusicInfo == null){
            mCachedMusicInfo = new ArrayList<>();
        }
        mCachedMusicInfo.clear();
        curSize = musicInfoList.size();
        mCachedMusicInfo = musicInfoList;
=======
    private void refreshCache(List<MusicInfo> musicInfoList){
        if (mCachedMusicInfo == null){
            mCachedMusicInfo = new LinkedHashMap<>();
        }
        mCachedMusicInfo.clear();
        for (MusicInfo musicInfo : musicInfoList){
            mCachedMusicInfo.put(new Long(musicInfo.getId()), musicInfo);
        }
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    }
}
